package com.hospital.controller;

import com.hospital.common.R;
import com.hospital.config.PreReadUploadConfig;
import com.hospital.utils.PictureCutUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import static com.hospital.utils.PictureCutUtil.dPic;

/**
 * @Author:晓风残月Lx
 * @Date: 2022/11/20 20:50
 */
@Controller
@RequestMapping("/hospital")
public class FileController {


    @Autowired
    PreReadUploadConfig uploadConfig;

    // doctors
    @ResponseBody
    @PostMapping("/doctors/upload")
    public R upload(@RequestParam("file") MultipartFile file) {
        // 原始文件名
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 使用UUID重新生成文件名，防止文件名称重复造成文件覆盖
        String fileName = UUID.randomUUID().toString() + suffix;
//        String filePath = System.getProperty("user.dir") + "/src/main/resources/static/files/doctor/" + fileName;
        String filePath = System.getProperty("user.dir") + "/files/images/" + fileName;
        try {
            file.transferTo(new File(filePath));
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("图片上传失败");
        }
        return R.success("/files/images/" + fileName);
    }

    @ResponseBody
    @DeleteMapping("/doctors/deletePicture")
    public R deleteDoctorPicture(@RequestParam("fileName") String fileName) {
        // String filePath = System.getProperty("user.dir") + "/src/main/resources/files/doctor/" + fileName;
//        String filePath = System.getProperty("user.dir") + "/src/main/resources/static" + fileName;
        String filePath = System.getProperty("user.dir") + "/files/images/" + fileName;
        File file = new File(filePath);
        //判断文件是否存在
        if (file.exists()) {
            boolean flag = false;
            flag = file.delete();
            if (flag) {
                System.out.println("成功删除图片" + file.getName());
            } else {
                System.out.println("删除失败");
            }
        } else {
            System.out.println("图片不存在，终止操作");
        }
        return R.success("success");
    }

    // patients
    @ResponseBody
    @PostMapping("/patients/upload")
    public R uploadPatientImg(@RequestParam("file[0]") MultipartFile file) {
        // 原始文件名
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 使用UUID重新生成文件名，防止文件名称重复造成文件覆盖
        String fileName = UUID.randomUUID().toString() + suffix;
//        String filePath = System.getProperty("user.dir") + "/src/main/resources/static/files/patient/" + fileName;
        String filePath = System.getProperty("user.dir") + "/files/images/" + fileName;
        System.out.println(System.getProperty("user.dir"));
        try {
            file.transferTo(new File(filePath));
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("图片上传失败");
        }
        return R.success(fileName);
    }

    @ResponseBody
    @DeleteMapping("/patients/deletePicture")
    public R deletePatientPicture(@RequestParam("fileName") String fileName) {
        String[] split = fileName.split(",");
        for (int i = 0; i < split.length; i++){
            // String filePath = System.getProperty("user.dir") + "/src/main/resources/files/patient/" + fileName;
//            String filePath = System.getProperty("user.dir") + "/src/main/resources/static/patient/" + split[i];
//            System.out.println(filePath);
            String filePath = System.getProperty("user.dir") + "/files/images/" + fileName;
            File file = new File(filePath);
            //判断文件是否存在
            if (file.exists()) {
                boolean flag = false;
                flag = file.delete();
                if (flag) {
                    System.out.println("成功删除图片" + file.getName());
                } else {
                    System.out.println("删除失败");
                }
            } else {
                System.out.println("图片不存在，终止操作");
            }
        }
        return R.success("success");
    }
}
